﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using WebBanThuoc.DTO;

namespace WebBanThuoc.DAO
{
    public class TinhTrangDonHang_DAO
    {
        private SqlConnection connection;
        //public bool hasData { set; get; }
        public TinhTrangDonHang_DAO(SqlConnection connection)
        {
            this.connection = connection;
        }

        private List<TinhTrangDonHang> getListBy(string whereCom, string orderCom)
        {
            List<TinhTrangDonHang> data = new List<TinhTrangDonHang>();
            string comStr = "Select * from [TinhTrangDonHang]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    while (dap.DaReader.Read())
                    {
                        TinhTrangDonHang obj = new TinhTrangDonHang();

                        obj.MaTinhTrang = dap.DaReader["MaTinhTrang"].ToString();
                        obj.TenTinhTrang = dap.DaReader["TenTinhTrang"].ToString();                       
                        obj.IsStatus = Convert.ToInt32(dap.DaReader["IsStatus"]);

                        data.Add(obj);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public List<TinhTrangDonHang> getListByKey()
        {
            return getListBy("", " [MaTinhTrang] asc");
        }

        public string createMaNhaSanXuat()
        {
            string result = "";
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, "SELECT [dbo].[fnc_CreateNew_MaTinhTrangDonHang] () as col");
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {
                    dap.DaReader.Read();
                    result = dap.DaReader["col"].ToString().Trim();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private int delete(string whereCom)
        {
            string comUpdate = "DELETE FROM [TinhTrangDonHang] ";
            if (whereCom.Trim() != "")
                comUpdate += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(this.connection);
                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comUpdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int deleteByKey(string MaTinhTrang)
        {
            return delete(" [MaTinhTrang]='" + MaTinhTrang + "' ");
        }

        public int add(TinhTrangDonHang obj)
        {
            string comQuery = "INSERT INTO [TinhTrangDonHang] ([MaTinhTrang],[TenTinhTrang]"
               + ",[IsStatus])"
               + " VALUES (@MaTinhTrang,@TenTinhTrang,@IsStatus)";

            try
            {
                IDataProvider dap = new DataProvider(connection);

                dap.AddParameters("@MaTinhTrang", obj.MaTinhTrang, System.Data.SqlDbType.NChar);
                dap.AddParameters("@TenTinhTrang", obj.TenTinhTrang, System.Data.SqlDbType.NVarChar);                
                dap.AddParameters("@IsStatus", obj.IsStatus, System.Data.SqlDbType.Int);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private int update(TinhTrangDonHang obj, string whereCom)
        {

            string comQuery = "UPDATE [TinhTrangDonHang] SET [MaTinhTrang]=@MaTinhTrang,[TenTinhTrang]=@TenTinhTrang"
               + ",[IsStatus]=@IsStatus ";
            if (whereCom.Trim() != "")
                comQuery += " where " + whereCom;
            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.AddParameters("@MaTinhTrang", obj.MaTinhTrang, System.Data.SqlDbType.NChar);
                dap.AddParameters("@TenTinhTrang", obj.TenTinhTrang, System.Data.SqlDbType.NVarChar);                
                dap.AddParameters("@IsStatus", obj.IsStatus, System.Data.SqlDbType.Int);

                return dap.ExecuteNonQuery_Command(System.Data.CommandType.Text, comQuery);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int updateByKey(TinhTrangDonHang data, string MaTinhTrang)
        {
            return update(data, " [MaTinhTrang]='" + MaTinhTrang + "' ");
        }

        private TinhTrangDonHang getBy(string whereCom, string orderCom)
        {
            TinhTrangDonHang data = null;
            string comStr = "Select * from [TinhTrangDonHang]";
            if (whereCom.Trim() != "")
                comStr += " where " + whereCom;
            if (orderCom.Trim() != "")
            {
                comStr += " order by " + orderCom;
            }

            try
            {
                IDataProvider dap = new DataProvider(connection);
                dap.ExecuteReader_Command(System.Data.CommandType.Text, comStr);
                //this.hasData = dap.HasData;
                if (dap.HasData)
                {

                    dap.DaReader.Read();
                    data = new TinhTrangDonHang();

                    data.MaTinhTrang = dap.DaReader["MaTinhTrang"].ToString();
                    data.TenTinhTrang = dap.DaReader["TenTinhTrang"].ToString();
                    data.IsStatus = Convert.ToInt32(dap.DaReader["IsStatus"]);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return data;
        }

        public TinhTrangDonHang getByKey(string MaTinhTrang)
        {
            return getBy(" [MaTinhTrang]='" + MaTinhTrang + "' ", " [MaTinhTrang] asc");
        }
    }
}